<?php


namespace app\admin\controller\Merchant;


use app\admin\controller\BaseController;
use app\admin\model\Merchant\MerchantModel;
use app\Utils\ImageUtil;
use app\Utils\Util;
use think\Request;

class Merchant extends BaseController
{

    /**
     * 分页查询
     * @param Request $request
     * @return array
     */
    public function getMerchantList(Request $request)
    {
        $param = $request->param();
        $where = [];
        $page = $param['page'];
        $limit = $param['limit'];
        $account = !empty($param['account']) ? $param['account'] : "";
        $audit_status = !empty($param['audit_status']) ? $param['audit_status'] : "";
        $is_show = !empty($param['is_show']) ? $param['is_show'] : "";
        if (empty($page)) {
            $page = 1;
        }
        if (empty($limit)) {
            $limit = 10;
        }

        if (!empty($account)) {
            array_push($where, ['account', 'like', '%' . $account . '%']);
        }
        if (!empty($audit_status)) {
            if ($audit_status == 1) {
                array_push($where, ['audit_status', '=', 0]);
            }
            if ($audit_status == 2) {
                array_push($where, ['audit_status', '=', 1]);
            }
            if ($audit_status == -1) {
                array_push($where, ['audit_status', '=', -1]);
            }
        }

        if (!empty($is_show)) {
            if ($is_show == 1) {
                array_push($where, ['is_show', '=', 0]);
            }
            if ($is_show == 2) {
                array_push($where, ['is_show', '=', 1]);
            }
        }

        $list = MerchantModel::getMerchantListWhere($page, $limit, $where);
        $count = MerchantModel::getMerchantCountWhere($where);
        foreach ($list as &$item){
            $item['mer_logo'] = ImageUtil::i()->replaceUrl($item['mer_logo']);
            $images = !empty($item['slider_img']) ? json_decode($item['slider_img'],true) : [];
            foreach ($images as &$image){
                $image = ImageUtil::i()->replaceUrl($image);
            }
            $item['slider_img'] = $images;
        }

        return Util::returnPageJson(self::CODE_DATA, $count, $list, "");

    }

    /**
     * 更多商家
     * @param Request $request
     * @return array
     */
    public function getMerchantMore(Request $request)
    {
        $list = MerchantModel::getMerchantWhereMore([['is_show','=',1]]);
        return Util::returnJson(self::CODE_DATA,$list);
    }

    /**
     * 添加
     * @param Request $request
     * @return array
     */
    public function addMerchant(Request $request)
    {
        $param = $request->param();
        $data = [
            'account' => !empty($param['account']) ? $param['account'] : "",
            'password' => md5($param['password']),
            'mer_name' => !empty($param['mer_name']) ? $param['mer_name'] : "",
            'mer_phone' => !empty($param['mer_phone']) ? $param['mer_phone'] : "",
            'brief' => !empty($param['brief']) ? $param['brief'] : "",
            'province_id' => !empty($param['province_id']) ? $param['province_id'] : 0,
            'city_id' => !empty($param['city_id']) ? $param['city_id'] : 0,
            'county_id' => !empty($param['county_id']) ? $param['county_id'] : 0,
            'mer_address' => $param['mer_address'],
            'mer_json' => !empty($param['mer_json']) ? json_encode($param['mer_json']) : "",
            'up_time' => date("Y-m-d H:i:s",time()),
            'add_time' => date("Y-m-d H:i:s",time())
        ];

        $merchant = MerchantModel::getMerchantWhereMore([['account','=',$param['account']]]);
        if(!empty($merchant)){
            return Util::returnJson(self::CODE_ERROR, null, "商户已存在");
        }

        //多图
        if (!empty($request->file('files'))) {
            $files = $request->file('files');
            $path = "/merchant/".$param['account']."/";
            $urls = ImageUtil::i()->upload($files, $path);
        }

        //图片上传(单图)
        if (!empty($request->file('file'))) {
            $file = $request->file('file');
            $path = "/merchant/logo/";
            $url = ImageUtil::i()->upload($file, $path);
        }
        if (!empty($url)) {
            $data['mer_logo'] = $url;
        }

        if (!empty($urls)) {
            $data['slider_img'] = json_encode($urls);
        }

        if (!empty($param['is_open']) && $param['is_open'] == "on") {
            $data['is_open'] = 1;
        }else{
            $data['is_open'] = 0;
        }

        if (!empty($param['is_show']) && $param['is_show'] == "on") {
            $data['is_show'] = 1;
        }else{
            $data['is_show'] = 0;
        }
        $id = MerchantModel::addMerchant($data);
        if ($id) {
            return Util::returnJson(self::CODE_SUCCESS, $id, "添加成功");
        } else {
            return Util::returnJson(self::CODE_ERROR, null, "添加失败");
        }
    }

    /**
     * 更新
     * @param Request $request
     * @return array
     */
    public function editMerchant(Request $request)
    {
        $param = $request->param();
        $save = [
            'mer_name' => !empty($param['mer_name']) ? $param['mer_name'] : "",
            'mer_phone' => !empty($param['mer_phone']) ? $param['mer_phone'] : "",
            'brief' => !empty($param['brief']) ? $param['brief'] : "",
            'province_id' => !empty($param['province_id']) ? $param['province_id'] : 0,
            'city_id' => !empty($param['city_id']) ? $param['city_id'] : 0,
            'county_id' => !empty($param['county_id']) ? $param['county_id'] : 0,
            'mer_address' => !empty($param['mer_address']) ? $param['mer_address'] : "",
            'mer_json' => !empty($param['mer_json']) ? json_encode($param['mer_json']) : "",
            'up_time' => date("Y-m-d H:i:s",time())
        ];

        if(!empty($param['password'])){
            $save['password'] = md5($param['password']);
        }

        //多图
        if (!empty($request->file('files'))) {
            $files = $request->file('files');
            $path = "/merchant/".$param['account']."/";
            $urls = ImageUtil::i()->upload($files, $path);
        }

        //图片上传(单图)
        if (!empty($request->file('file'))) {
            $file = $request->file('file');
            $path = "/merchant/logo/";
            $url = ImageUtil::i()->upload($file, $path);
        }

        if (!empty($url)) {
            $save['mer_logo'] = $url;
        }

        if (!empty($urls)) {
            $save['slider_img'] = json_encode($urls);
        }

        if (!empty($param['is_open']) && $param['is_open'] == "on") {
            $save['is_open'] = 1;
        }else{
            $save['is_open'] = 0;
        }

        if (!empty($param['is_show']) && $param['is_show'] == "on") {
            $save['is_show'] = 1;
        }else{
            $save['is_show'] = 0;
        }

        $flag = MerchantModel::updateMerchantWhere([['id', '=', $param['id']]], $save);
        if ($flag) {
            return Util::returnJson(self::CODE_SUCCESS, null, "修改成功");
        } else {
            return Util::returnJson(self::CODE_ERROR, null, "修改失败1");
        }
    }

    /**
     * 更新状态
     * @param Request $request
     * @return array
     */
    public function editMerchantType(Request $request)
    {
        $param = $request->param();
        if (!empty($param['is_open']) && $param['is_open'] == "on") {
            $save['is_open'] = 1;
        }else{
            $save['is_open'] = 0;
        }

        if (!empty($param['audit_status'])) {
            $save['audit_status'] = $param['audit_status'];
        }else{
            $save['audit_status'] = 0;
        }

        if (!empty($param['is_show']) && $param['is_show'] == "on") {
            $save['is_show'] = 1;
        }else{
            $save['is_show'] = 0;
        }

        $flag = MerchantModel::updateMerchantWhere([['id', '=', $param['id']]], $save);
        if ($flag) {
            return Util::returnJson(self::CODE_SUCCESS, null, "修改成功");
        } else {
            return Util::returnJson(self::CODE_ERROR, null, "修改失败1");
        }
    }



}
